package org.ax.service.sys.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ax.mapper.sys.UserMapper;
import org.ax.mapper.sys.UserRoleMapper;
import org.ax.pojo.sys.User;
import org.ax.pojo.sys.UserRole;
import org.ax.service.sys.UserRoleService;
import org.ax.service.sys.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-02-23-14:49
 */
@Service
public class IUserRoleService extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Override
    public String toUserIdFindRole(String userId) {
        QueryWrapper<UserRole> queryWrapper=new QueryWrapper<UserRole>();
        queryWrapper.eq("sys_user_id", userId);
        return getBaseMapper().selectOne(queryWrapper).getSysRoleId();

    }

    @Override
    public boolean addUserRole(UserRole userRole) {
        return getBaseMapper().insert(userRole)>0;
    }

    @Override
    public boolean updateUserRole(UserRole userRole) {
        UpdateWrapper<UserRole> updateWrapper=new UpdateWrapper<UserRole>();
        updateWrapper.eq("sys_user_id", userRole.getSysUserId());
        return getBaseMapper().update(userRole, updateWrapper)>0;
    }

    @Override
    public boolean deletesUserRole(String[] ids) {
        List<String> roleId=new ArrayList<>();
        for (String id : ids) {
            roleId.add(toUserIdFindRole(id));
        }
        UpdateWrapper<UserRole> updateWrapper=new UpdateWrapper<UserRole>();
        updateWrapper.in("sys_user_id",roleId);
        return getBaseMapper().delete(updateWrapper)>0;
    }
}
